﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BaW
{
    public partial class BaW : Form
    {
        #region Private members
        private BaWScreen myProjectionScreen;
        private FontDialog myFontDialog = null;

        #endregion

        #region Constructors
        public BaW()
        {
            InitializeComponent();
        }

        #endregion

        #region Handlers
        private void myListMonitorsButton_Click(object sender, EventArgs e)
        {
            // First, clear the text in the output textbox
            myDebugTextBox.Text = String.Empty;
            
            // Get the list of all screens
            Screen[] aScreens = Screen.AllScreens;

            StringBuilder aStringBuilder = new StringBuilder();
            // For each screen get its properties and add it to the string builder
            foreach (Screen aScreen in aScreens)
            {
                String aDeviceName = System.Text.RegularExpressions.Regex.Replace(aScreen.DeviceName, @"[^a-z0-9A-Z_]", "");
                aStringBuilder.AppendLine(string.Format("Device Name: {0}", aDeviceName));

                aStringBuilder.AppendLine(string.Format("Primary monitor?: {0}", aScreen.Primary.ToString()));
                aStringBuilder.AppendLine(string.Format("Working Area: {0}", aScreen.WorkingArea.ToString()));
                aStringBuilder.AppendLine(string.Format("Bounds: {0}", aScreen.Bounds.ToString()));
                aStringBuilder.AppendLine(string.Format("BitsPerPixel: {0}", aScreen.BitsPerPixel.ToString()));
                aStringBuilder.AppendLine();
            }

            // Display final text in the output text box
            myDebugTextBox.Text = aStringBuilder.ToString();
        }        

        private void myClearButton_Click(object sender, EventArgs e)
        {
            DoGoOffline();            
        }

        private void OnLoad(object sender, EventArgs e)
        {
            // Load available displays
            LoadAvailableDisplays();

            // If there is at least one display available (which should alway be!), then select it
            if (0 < myAvailableDisplaysComboBox.Items.Count)
            {
                myAvailableDisplaysComboBox.SelectedItem = myAvailableDisplaysComboBox.Items[0];
            }
        }

        private void OnDisplaySelected(object sender, EventArgs e)
        {
            string aNewDisplay = (string)myAvailableDisplaysComboBox.SelectedItem;

        }

        private void OnOutputTextChanged(object sender, EventArgs e)
        {
            if (myGoLiveCheckBox.Checked)
            {
                myProjectionScreen.ShowText(myOutputTextBox.Text);
            }
        }

        private void OnGoLive(object sender, EventArgs e)
        {
            if (myGoLiveCheckBox.Checked)
            {
                DoGoLive();
            }
            else
            {
                DoGoOffline();
            }                                  
        }        

        #endregion

        #region Helpers
        /// <summary>
        /// Method for hiding of the presentation screen
        /// </summary> 
        private void DoGoOffline()
        {
            myProjectionScreen.Hide();
        }

        /// <summary>
        /// Method for showing presentation screen
        /// </summary>
        private void DoGoLive()
        {
            Screen aScreen = GetScreenToBeam((string)myAvailableDisplaysComboBox.SelectedItem);

            if (null == aScreen)
            {
                return;
            }

            if (null == myProjectionScreen)
            {
                myProjectionScreen = new BaWScreen();
            }

            myProjectionScreen.Bounds = aScreen.Bounds;
            myProjectionScreen.Location = new Point(aScreen.Bounds.X, aScreen.Bounds.Y);
            myProjectionScreen.Visible = true;
            
            myProjectionScreen.ShowText(myOutputTextBox.Text);            
        }

        /// <summary>
        /// Gets the screen, where the display name is the ID of the screen
        /// </summary>
        /// <param name="theDeviceName">The Device Name of the screen we want</param>
        /// <returns>The screen with given Display Name or the null, if not available</returns>
        private Screen GetScreenToBeam(string theDeviceName)
        {
            if (null == theDeviceName)
            {
                return null;
            }

            // Get the list of all screens
            Screen[] aScreens = Screen.AllScreens;
            // For each screen get its properties and add it to the string builder
            foreach (Screen aScreen in aScreens)
            {
                String aDeviceName = System.Text.RegularExpressions.Regex.Replace(aScreen.DeviceName, @"[^a-z0-9A-Z_]", "");

                if (aDeviceName == theDeviceName)
                {
                    return aScreen;
                }
            }

            // Return null, if no screen was picked
            return null;            
        }

        /// <summary>
        /// Load available Screens into the ComboBox
        /// </summary>
        private void LoadAvailableDisplays()
        {
            // First, clear the list of available displays
            myAvailableDisplaysComboBox.Items.Clear();

            // Get the list of all screens
            Screen[] aScreens = Screen.AllScreens;

            // For each screen get its properties and add it to the string builder
            foreach (Screen aScreen in aScreens)
            {
                String aDeviceName = System.Text.RegularExpressions.Regex.Replace(aScreen.DeviceName, @"[^a-z0-9A-Z_]", "");
                myAvailableDisplaysComboBox.Items.Add(aDeviceName);
            }
        }

        #endregion        

        private void OnChangeFontClicked(object sender, EventArgs e)
        {
            if (null == myFontDialog)
            {
                myFontDialog = new FontDialog();
            }
            
            DialogResult aResult = myFontDialog.ShowDialog();
            if (DialogResult.OK == aResult)
            {
                myOutputTextBox.Font = myFontDialog.Font;
                myProjectionScreen.TextFont = myFontDialog.Font;
            }
        }

        private void OnXValueScroll(object sender, EventArgs e)
        {
            myProjectionScreen.StartX = myXTrackBar.Value * 10;
        }

        private void OnYValueScroll(object sender, EventArgs e)
        {
            myProjectionScreen.StartY = myYTrackBar.Value * 10;
        }
    }
}
